package com.qc.ayouth.shop.admin.service.shop.impl;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.IdWorker;
import com.qc.ayouth.shop.admin.beans.dto.CategoryDto;
import com.qc.ayouth.shop.admin.beans.dto.CategorySimpleDto;
import com.qc.ayouth.shop.admin.service.shop.AdminCategoryService;
import com.qc.ayouth.shop.admin.service.user.AdminAuthService;
import com.qc.ayouth.shop.core.beans.bo.ServiceResultBO;
import com.qc.ayouth.shop.core.beans.bo.ValidResultBO;
import com.qc.ayouth.shop.core.constants.CommonConst;
import com.qc.ayouth.shop.core.utils.BeanCopyUtil;
import com.qc.ayouth.shop.dbs.entity.TShopCategory;
import com.qc.ayouth.shop.dbs.entity.TSystemAdmin;
import com.qc.ayouth.shop.dbs.entity.TSystemMenu;
import com.qc.ayouth.shop.dbs.entity.TSystemRole;
import com.qc.ayouth.shop.dbs.service.shop.TShopCategoryService;
import com.qc.ayouth.shop.dbs.service.system.TSystemMenuService;
import com.qc.ayouth.shop.dbs.service.system.TSystemRoleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by Haojg on 2020/7/9
 */
@Service
public class AdminCategoryServiceImpl implements AdminCategoryService {
    @Autowired
    TShopCategoryService tShopCategoryService;

    @Override
    public ServiceResultBO getGoodsCategoryList() {
        ServiceResultBO bo = new ServiceResultBO();
        // 1.获取一级列表
        List<TShopCategory> catList = tShopCategoryService.getCategoryMain();
        List<CategoryDto> categoryDtoList = new ArrayList<>();
        for (TShopCategory cate : catList) {
            CategoryDto categoryDto = BeanCopyUtil.copy(cate, CategoryDto.class);

            // 2.获取分类下的二级分类
            List<TShopCategory> subCatList = tShopCategoryService.getCategoryChildren(cate.getId());
            List<CategoryDto> subCatDtoList = BeanCopyUtil.copyList(subCatList, CategoryDto.class);
            categoryDto.setChildren(subCatDtoList);

            // 3.返回分类
            categoryDtoList.add(categoryDto);
        }

        // 返回组装后的结果
        bo.add("categoryList", categoryDtoList);
        bo.success();
        return bo;
    }

    @Override
    public List<CategoryDto> getCategoryList() {
        List<TShopCategory> catList = tShopCategoryService.getCategoryMain();
        List<CategoryDto> categoryDtoList = BeanCopyUtil.copyList(catList, CategoryDto.class);
        return categoryDtoList;
    }

    @Override
    public List<CategoryDto> getCategoryChildrenList(Long catId) {
        List<TShopCategory> subCatList = tShopCategoryService.getCategoryChildren(catId);
        List<CategoryDto> subCatDtoList = BeanCopyUtil.copyList(subCatList, CategoryDto.class);
        return subCatDtoList;
    }

    @Override
    public List<CategorySimpleDto> getMainCategoryList() {
        List<TShopCategory> catList = tShopCategoryService.getCategoryMain();
        return BeanCopyUtil.copyList(catList, CategorySimpleDto.class);
    }

    @Override
    public ValidResultBO validCreateCategory(JSONObject params) {
        ValidResultBO bo = new ValidResultBO();
        String name = params.getString("name");
        if (StringUtils.isEmpty(name)) {
            bo.badArgument();
            return bo;
        }

        String level = params.getString("catLevel");
        if (StringUtils.isEmpty(level)) {
            bo.badArgument();
            return bo;
        }
        if (!level.equals("L1") && !level.equals("L2")) {
            bo.badArgumentValue();
            return bo;
        }

        Long pid = params.getLong("pid");
        if (level.equals("L2") && (pid == null)) {
            bo.badArgument();
            return bo;
        }
        bo.ok();
        return bo;
    }

    @Override
    public Boolean saveCategory(JSONObject params) {
        TShopCategory shopCategory = new TShopCategory();
        shopCategory.setId(IdWorker.getId());
        shopCategory.setName(params.getString("name"));
        shopCategory.setKeywords(params.getString("keywords"));
        shopCategory.setCatDesc(params.getString("catDesc"));
        if (StringUtils.isEmpty(params.getString("pid"))) {
            shopCategory.setPid(0L);
        } else {
            shopCategory.setPid(params.getLong("pid"));
        }
        if (!StringUtils.isEmpty(params.getString("sortOrder"))) {
            shopCategory.setSortOrder(params.getInteger("sortOrder"));
        }
        shopCategory.setIconUrl(params.getString("iconUrl"));
        shopCategory.setPicUrl(params.getString("picUrl"));
        shopCategory.setCatLevel(params.getString("catLevel"));
        shopCategory.setIsDeleted(CommonConst.IS_DELETED_NONE);
        return tShopCategoryService.save(shopCategory);
    }

    @Override
    public Boolean updateCategory(JSONObject params) {
        TShopCategory shopCategory = new TShopCategory();
        shopCategory.setId(params.getLong("id"));
        shopCategory.setName(params.getString("name"));
        shopCategory.setKeywords(params.getString("keywords"));
        shopCategory.setCatDesc(params.getString("catDesc"));
        if (StringUtils.isEmpty(params.getString("pid"))) {
            shopCategory.setPid(0L);
        } else {
            shopCategory.setPid(params.getLong("pid"));
        }
        if (!StringUtils.isEmpty(params.getString("sortOrder"))) {
            shopCategory.setSortOrder(params.getInteger("sortOrder"));
        }
        shopCategory.setIconUrl(params.getString("iconUrl"));
        shopCategory.setPicUrl(params.getString("picUrl"));
        shopCategory.setCatLevel(params.getString("catLevel"));
        shopCategory.setIsDeleted(CommonConst.IS_DELETED_NONE);
        return tShopCategoryService.updateById(shopCategory);
    }

    @Override
    public Boolean deleteCategory(Long id) {
        TShopCategory shopCategory = new TShopCategory();
        shopCategory.setId(id);
        shopCategory.setIsDeleted(CommonConst.IS_DELETED_DELETED);
        return tShopCategoryService.updateById(shopCategory);
    }
}
